from celery import shared_task
from django.db.models import F
from .models import Article

@shared_task
def increment_views(article_id):
    """
    异步增加文章浏览量
    使用 F() 表达式确保并发安全
    """
    try:
        Article.objects.filter(id=article_id).update(views=F('views') + 1)
        return True
    except Exception as e:
        print(f"Error incrementing views for article {article_id}: {str(e)}")
        return False